package com.study.hot.jetpack.lifecycle.use_in_mvp

import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent

/**
 * <pre>
 *     author : slx
 *     time   : 2021/02/24
 *     desc   :
 * </pre>
 */
class LifePresenter(val view: IView) : LifecycleObserver {

    // 在耗时任务完成(查询数据库、网络请求)后，检查生命周期，再回调UI，防止内存泄漏
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun getDataOnStart(lifecycleOwner: LifecycleOwner) {
        if (lifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
            Log.i("fauks", "presenter 请求数据")
            view.show()
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun hideDataOnStop() {
        Log.i("fauks", "presenter 释放资源")
        view.hide()
    }
}